package com.tgy.studentmanagement.service.impl;

import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.view.PoiBaseView;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tgy.studentmanagement.VO.QueryStudentVo;
import com.tgy.studentmanagement.VO.StatisticVO;
import com.tgy.studentmanagement.common.R;
import com.tgy.studentmanagement.ecxeptionHandel.BasicErrorType;
import com.tgy.studentmanagement.ecxeptionHandel.GlobalException;
import com.tgy.studentmanagement.entity.Students;
import com.tgy.studentmanagement.dao.StudentsDao;
import com.tgy.studentmanagement.service.StudentsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.HashMap;
import java.util.List;

/**
 * (Students)表服务实现类
 *
 * @author
 * @since 2023-04-12 14:17:29
 */
@Service("studentsService")
@Slf4j
public class StudentsServiceImpl implements StudentsService {
    @Resource
    private StudentsDao studentsDao;


    /**
     * 新增数据
     *
     * @param students 实例对象
     * @return 实例对象
     */
    @Override
    public Students insert(Students students) {
        this.studentsDao.insert(students);
        return students;
    }

    /**
     * 修改数据
     *
     * @param students 实例对象
     * @return 实例对象
     */
    @Override
    public Students update(Students students) {
        this.studentsDao.update(students);
        return null;
    }

    /**
     * 通过主键删除数据
     *
     * @param studentId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteByStudentId(String studentId) {
        return this.studentsDao.deleteByStudentId(studentId) > 0;
    }

    @Override
    public List<Students> query(QueryStudentVo students) {

        return this.studentsDao.query(students);
    }

    @Override
    public StatisticVO getStatisticInfo() {
        StatisticVO statisticVO = new StatisticVO();
        int size = this.studentsDao.query(null).size();
        statisticVO.setSum(size);
        System.out.println((long)size);
        Double ratio = this.studentsDao.selectCount(new QueryWrapper<Students>().eq("gender", "男")).doubleValue() / (long)size;
        statisticVO.setGenderRatio(ratio);
        statisticVO.setCan(this.studentsDao.queryClassAndNum());
        return statisticVO;
    }

    @Override
    public int insertBatch(List<Students> students) {
        return this.studentsDao.insertBatch(students);
    }

    @Override
    public void exportMemberList(HttpServletRequest request, HttpServletResponse response) {
        HashMap<String, Object> map = new HashMap<>(4);
        List<Students> students = this.studentsDao.selectList(null);
        ExportParams params = new ExportParams("学生信息", "学生信息", ExcelType.XSSF);
        map.put(NormalExcelConstants.DATA_LIST, students);
        map.put(NormalExcelConstants.CLASS, Students.class);
        map.put(NormalExcelConstants.PARAMS, params);
        map.put(NormalExcelConstants.FILE_NAME, "StudentsList");
        PoiBaseView.render(map, request, response, NormalExcelConstants.EASYPOI_EXCEL_VIEW);
    }


    @Override
    public int importMemberList(MultipartFile file) {
        ImportParams params = new ImportParams();
        params.setTitleRows(1);
        params.setHeadRows(1);
        try {
            List<Students> list = ExcelImportUtil.importExcel(
                    file.getInputStream(),
                    Students.class, params);
            return this.studentsDao.insertBatch(list);
        } catch (DuplicateKeyException e) {
            log.error(e.getMessage());
            throw new GlobalException(BasicErrorType.UPLOD_ERROR);
        }catch (IOException e) {
            log.error(e.getMessage());
            throw new GlobalException(BasicErrorType.IO_ERROR);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new GlobalException(BasicErrorType.UNKNOW_ERROR);
        }
    }
}
